window.onload = function() {
	var fxBascule = function (event) {
		switchTo(event.target);
	} 
	var submitStrateur = function (event) {
		var strateurs = document.getElementById('strateurs');
		for (o = 0 ; o < strateurs.length; o++) {
			strateurs[o].selected = true;
		}
	}
	document.getElementById("oneToStrateurs").addEventListener('click', fxBascule, false);
	document.getElementById("allToStrateurs").addEventListener('click', fxBascule, false);
	document.getElementById("oneToMembres").addEventListener('click', fxBascule, false);
	document.getElementById("allToMembres").addEventListener('click', fxBascule, false);
	document.getElementById('frm_params').addEventListener('submit', submitStrateur, false);
}

function switchTo(target) {
	switch(target.id) {
	case "oneToStrateurs":
		var source = document.getElementById('membres');
		var destination = document.getElementById('strateurs');
		moveTo(source, destination, true);
		break;

	case "allToStrateurs":
		var source = document.getElementById('membres');
		var destination = document.getElementById('strateurs');
		moveTo(source, destination, false);		
		break;

	case "oneToMembres":
		var destination = document.getElementById('membres');
		var source = document.getElementById('strateurs');
		moveTo(source, destination, true);	
		break;

	case "allToMembres":
		var destination = document.getElementById('membres');
		var source = document.getElementById('strateurs');
		moveTo(source, destination, false);	
		break;
	}
}

function moveTo(source, destination, selected) {
	var nbElement = source.childNodes.length;
	for(i = 0; i <nbElement; i++) {
		if (source.childNodes[i] != null && (selected == false || (selected == true && source.childNodes[i].selected == true))) {
			if (source.childNodes[i].nodeName == 'OPTION') {
				var opt = document.createElement('option');
				var accountId = source.childNodes[i].value;
				var accountName = source.childNodes[i].textContent;
				var pseudo = document.createTextNode(accountName);
				opt.value = accountId;
				opt.appendChild(pseudo);
				destination.appendChild(opt);
			}
			else if(source.childNodes[i].nodeName == '#text') {
				var valeur = source.childNodes[i].data;
				var texte = document.createTextNode(valeur);
				destination.appendChild(texte);
			}
			source.removeChild(source.childNodes[i]);
			i--;
		}
	}
	
}

function removeSelected(target, selected) {
	for (s=0; s < selected.length; s++) {
		target.childNodes[selected[i]].removeChild();
	}
}